package com.qipay.api.util;


import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class ContentUtil {
    /**
     * 获取需要签名的字符串
     * @param object
     * @return
     */
    public static String getChackdata(Object object){
        //对象转map去除空值
        Map<String, Object> orderMap = null;
        if(object instanceof Map){
            orderMap = (Map)object;
        }else {
            orderMap =  BeanUtils.bean2Map(object);
        }
        System.out.println("orderMap=="+orderMap);

        orderMap.remove("sign");
        orderMap.remove("signType");
        StringBuffer orderBuffer = new StringBuffer();
        MapKeyComparator mapkey = new MapKeyComparator();
        Map<String, Object> orderSortMap = mapkey.sortMapByKey(orderMap);
        System.out.println("orderSortMap=="+orderSortMap);
        Iterator<Map.Entry<String, Object>> orderIt = orderSortMap.entrySet().iterator();
        System.out.println("orderIt=="+orderIt);
        while (orderIt.hasNext()) {
            Map.Entry<String, Object> entry = orderIt.next();
            if (entry.getValue() != null && StringUtils.isNotBlank(entry.getValue().toString())) {
                orderBuffer.append(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        String chackdata = orderBuffer.toString();
        System.out.println("chackdata=="+chackdata);
        chackdata = chackdata.substring(0, chackdata.length() - 1);
        System.out.println("chackdata=="+chackdata);
        return chackdata;
    }



    /**
     *  map去空
     */
    public static Map<String, String> mapRemoveNull(Map<String, String> map){
        Map<String, String> newMap = new HashMap<String,String>();
        for(Map.Entry<String, String> entry : map.entrySet()){
            if(null != entry.getValue() && StringUtils.isNotBlank(entry.getValue())){
                newMap.put(entry.getKey(),entry.getValue());
            }
        }
        return newMap;
    }

    /**
     * 设置返回头部;
     *
     * @param response
     * @param fileName
     */
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                if(null != fileName){
                    fileName = new String(fileName.getBytes(), "ISO-8859-1");
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //text/xml
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
